package com.ymatou.iapi.liveinfo.testcase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveinfo.parameter.GetSellerBoutiqueLiveBean;
import com.ymatou.iapi.liveinfo.service.GetSellerBoutiqueLiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.database.mongo.LiveModelWapper;
import com.ymttest.database.mongo.jproduct.LivesWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*获取卖家当前正在进行的直播
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerBoutiqueLive{
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");
	private static GetSellerBoutiqueLiveBean bean;
	private static GetSellerBoutiqueLiveCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取卖家当前正在进行的直播");
	}

	@Before
	public void caseUp() {
		bean=new GetSellerBoutiqueLiveBean();
		call=new GetSellerBoutiqueLiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBoutiqueLive_001() {
		Logger.start(true,"返回正确-卖家Id正确，获取卖家当前正在进行的直播");
		try {
			JSONObject js = LiveCallService.createLiveInProgress("wesper", 3383,1) ;
			//根据 活动id  lid 查找活动列表
			LivesWapper wapper = new LivesWapper();
			bean.setSellerId(sellerId);
			call.setData(bean);
			call.callService();	
//			GetVerify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_GetSellerBoutiqueLive_004() {
//		Logger.start(true,"返回正确-卖家Id正确，直播状态-未生效，获取卖家当前正在进行的直播");
//		try {
//			JSONObject object=LiveCallService.createLiveNotInEffect(sellerName,sellerId);
//			Integer liveId=object.getJSONObject("Data").getInt("ActivityId");
//			List<Integer> livelst=new ArrayList();
//			livelst.add(liveId);
//			//根据 活动id  lid 查找活动列表
//			LivesWapper wapper = new LivesWapper();
//			bean.setSellerId(sellerId);
//			call.setData(bean);
//			call.callService();	
//			GetVerify(livelst,wapper);
//		
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetSellerBoutiqueLive_002() {
		Logger.start(false,"返回异常-卖家Id不正确，获取卖家当前正在进行的直播");
		try {
			bean.setSellerId(999999);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals(200, call.getCode(), "判断接口code是否为400");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回信息");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyIsNull(call.getDataIsNull(), "判断Data为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetSellerBoutiqueLive_003() {
		Logger.start(false,"返回异常-卖家Id为空，获取卖家当前正在进行的直播");
		try {
			bean.setSellerId(null);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals(400, call.getCode(), "判断接口code是否为400");
			Logger.verifyEquals("错误的请求参数|无效的买手编号",call.getMsg(), "判断Msg返回信息");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyIsNull(call.getDataIsNull(), "判断Data为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSellerBoutiqueLive_004() {
		Logger.start(true,"返回正确-卖家Id正确，非精品直播返回null");
		try {
			List<Integer> livelst=LiveCallService.createLivesInProcessAndInFuture(sellerId,sellerName,1,1);
			for (Integer lid : livelst) {
				 String cover="http://m.ymatou.com/"+TestDataManager.getRandomUUID().toString();
				 new LivesWapper().updateCover(lid.toString(), cover);
			}
			//根据 活动id  lid 查找活动列表
			LivesWapper wapper = new LivesWapper();
			bean.setSellerId(sellerId);
			call.setData(bean);
			call.callService();	
			Logger.verifyEquals(200, call.getCode(), "判断接口code是否为400");
			Logger.verifyEquals("错误的请求参数|无效的买手编号",call.getMsg(), "判断Msg返回信息");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyIsNull(call.getDataIsNull(), "判断Data为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void GetVerify(List<Integer> livelst) throws Exception
	{
		LivesWapper wapper=new LivesWapper ();
		if(livelst.size()>0)
		//for(int i=0;i<livelst.size();i++)
        {
            Map<String,Object> lst=wapper.getLivesBylid(livelst.get(0));
			Logger.verifyEquals(200, call.getCode(), "判断code是否为200");
			Logger.verifyEquals("操作成功",call.getMsg(), "判断Msg返回操作成功");
			Logger.verifyEquals(0,call.getBCode(), "判断操作成功时 业务代码是否0");
			Logger.verifyNotNull(call.getServerTime(), "判断服务器时间是否为空");
			Logger.verifyEquals(lst.get("title"), call.getData().getString("Title"), "验证直播title是否与mongodb一致");
			Logger.verifyEquals(lst.get("vcover"), call.getData().getString("VideoCover"), "验证直播VideoCover是否与mongodb一致");
			Logger.verifyEquals(lst.get("vurl"), call.getData().getString("VideoUrl"), "验证直播VideoUrl是否与mongodb一致");
			Logger.verifyEquals(lst.get("action").toString(), call.getData().getString("Action").toString(), "验证直播Action是否与mongodb一致");
			Logger.verifyEquals(lst.get("lid").toString(), call.getData().getString("ActivityId").toString(), "验证直播ActivityId是否与mongodb一致");
			Logger.verifyEquals(lst.get("name"), call.getData().getString("ActivityName"), "验证直播ActivityName是否与mongodb一致");
			Logger.verifyEquals(lst.get("pic"), call.getData().getString("ActivityPicture"), "验证直播ActivityPicture是否与mongodb一致");
			Logger.verifyEquals(lst.get("cover"), call.getData().getString("LiveCover"), "验证直播LiveCover是否与mongodb一致");
			
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get("add")).toString(), call.getData().getString("AddTime").toString(), "验证直播AddTime是否与mongodb一致");
			Logger.verifyEquals(lst.get("country"), call.getData().getString("Country"), "验证直播Country是否与mongodb一致");
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get("end")), call.getData().getString("EndTime"), "验证直播EndTime是否与mongodb一致");
			Logger.verifyEquals(lst.get("sid").toString(), call.getData().getString("SellerId").toString(), "验证直播SellerId是否与mongodb一致");
			Logger.verifyEquals(lst.get("addr"), call.getData().getString("ShopAddress"), "验证直播ShopAddress是否与mongodb一致");
			Logger.verifyEquals((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get("start")), call.getData().getString("StartTime"), "验证直播StartTime是否与mongodb一致");
			Logger.verifyEquals(lst.get("content"), call.getData().getString("ActivityInfo"), "验证直播ActivityInfo是否与mongodb一致");
			String start=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get("start"));
			String end=(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(lst.get("end"));
			Logger.verifyEquals(GetActivityStatusText(lst,start,end), call.getData().getString("ActivityStatusText"), "验证直播ActivityStatusText是否与mongodb一致");
		 }
	}
	private String GetActivityStatusText(Map<String,Object> lst,String start1,String end1) throws ParseException, JSONException
	{
		String text="";
		Date currentTime = new Date();

		if((int)lst.get("action")==1 )
		{
			Calendar calendar  =  new   GregorianCalendar(); 
			calendar.setTime(currentTime); 
			calendar.add(calendar.HOUR,1);
			Date newCurrentTime=calendar.getTime();
			
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date start=format.parse(start1);
			Date end=format.parse(end1);
			if((boolean)lst.get("confirm")==true && ((currentTime.after(start)|| currentTime==start)&& (end.after(currentTime)|| currentTime==end)))
				text="直播中";
			else if((boolean)lst.get("confirm")==true && start.after(currentTime) && end.after(start))
				text="即将开始";
			else if((boolean)lst.get("confirm")==false && end.after(newCurrentTime))
				text="未生效";
			else
				text="已结束";
		}
		return text;
	}
}
